<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ include file="/taglibs.jsp"%>
<html>
<head>
<title>IT管理后台</title>
<link rel="stylesheet" type="text/css" href="/js/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="/js/themes/icon.css">
<script type="text/javascript" src="/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="/js/jquery.easyui.min.js"></script>
<script type="text/javascript" src="/js/easyui_validatebox.js"></script>
<script type="text/javascript" src="/js/locale/easyui-lang-zh_CN.js"></script>
<script src="/js/print.js" type="text/javascript"></script>
<script>
var sfyxData = [{id:'1',mc:'是'},{id:'0',mc:'否'}];
var tag = null;
var editId = null;
		$(function(){
			$('#test').treegrid({
				title:'&nbsp;',
				fit:true,
				fitColumns:true,
				singleSelect:true,
				border:false,
				rownumbers: true,
				animate:true,
				striped:true,
				editable:true,
				collapsible:true,
				url:'/itmanager/yh/qxwhjson',
				idField:'id',
				treeField:'mc',
				frozenColumns:[[
	                {title:'菜单名称',field:'mc',width:250,editor:{type:'validatebox',options:{required:true}}}
				]],
				columns:[[
					{field:'url',title:'URL',width:150,editor:{type:'validatebox'}},
					{field:'sfyx',title:'是否有效',width:80,formatter:function(value,rowData,rowIndex){
						if("0" == value){
							return '<font color="red">否</font>';
						}if("1" == value){
							return '是';
						}
					},editor:{type:'combobox',options:{editable:false,data:sfyxData,required:true,valueField:'id',textField:'mc',required:true}}},
					{field:'xssx',title:'显示顺序',width:80,editor:{type:'numberbox',options:{min:1,required:true}}},
					{field:'gnxx',title:'功能信息',width:300}
				]],
				toolbar:[
				   {
					   id:'1',
					   text:'添加',
					   disabled:true,
					   iconCls:'icon-add',
					   handler:function(){
						   var node = $('#test').treegrid('getSelected');
						   if(tag==null){
							   $('#test').treegrid('append',{
								   parent:(node?node.id:null),
								   data:[{
									   id:'x',
									   mc:'',
									   url:'',
									   sfyx:'1',
									   xssx:''
									}]
							   });
							   editId = 'x';
							   $("#test").treegrid('unselectAll');
							   $("#test").treegrid('select',editId);
							   $("#test").treegrid('beginEdit',editId);
							   tag = 'add';
						   }else{
							   $.messager.alert('友情提示','<br/>不允许增加，存在可编辑行！','error');
						   }
					   }
				   },
				   '-',
				   {
					   id:'2',
					   text:'修改',
					   disabled:true,
					   iconCls:'icon-edit',
					   handler:function(){
						   var node = $('#test').treegrid('getSelected');
						   if(tag == null ){
							   if(node){
							   		$("#test").treegrid('beginEdit',node.id);
							   		tag = 'edit';
							   		editId = node.id;
							   }
						   }else{
							   $.messager.alert('友情提示','<br/>不允许修改，存在可编辑行！','error');
						   }
					   }
				   },
				   '-',
				   {
					   id:'3',
					   text:'删除',
					   disabled:true,
					   iconCls:'icon-remove',
					   handler:function(){
						   var node = $('#test').treegrid('getSelected');
						   if(node){
							   $.ajax({
								   url:'/itmanager/yh/qxwhcz?tag=delete',
								   type:'post',
								   data:node,
								   success:function(data){
									   var parentNode = $('#test').treegrid('getParent',node.id);
									   if(parentNode){
									  		$('#test').treegrid('reload',parentNode.id);
									   }else{
										    $('#test').treegrid('remove',node.id);
									   }
									   $.messager.alert('友情提示','<br/>删除成功！','right');
									   tag = null;
									   editId = null;
								   },
								   error:function(data){
										 $.messager.alert('友情提示','<br/>删除失败！','error');
								   }
							   });
						   }else{
							   $.messager.alert('友情提示','<br/>先选中记录！','error');
						   }
					   }
				   },
				   '-',
				   {
					   id:'6',
					   text:'保存',
					   disabled:true,
					   iconCls:'icon-save',
					   handler:function(){
						  var node = $('#test').treegrid('getSelected');
						  if(node && (node.id == editId)){
							  $('#test').treegrid('endEdit',editId);
						  }else{
							  $.messager.alert('友情提示','<br/>操作有误！','error');
						  }
						  $('#test').treegrid('unselectAll');
					   }
				   },
				   '-',
				   {
					   id:'7',
					   text:'取消',
					   disabled:true,
					   iconCls:'icon-undo',
					   handler:function(){
						   if(tag == 'add'){
						   		$('#test').treegrid('remove','x');
						   }else{
							   var node = $('#test').treegrid('getSelected');
							   if(node){
							  	 	$('#test').treegrid('cancelEdit',node.id);
							   } 
						   }
						   $('#test').treegrid('unselectAll');
						   tag = null;
						   editId = null;
					   }
				   },'-',
				   {
					   id:'8',
					   text:'页面按钮设置',
					   disabled:true,
					   iconCls:'icon-save',
					   handler:function(){
					  		var row = $('#test').treegrid('getSelected');
					  		if(row){
						   		$("#jtcz").attr("src","/itmanager/yh/qxgnwh?qxid="+row.id);
			 					$('#gnsz').dialog('open');
			 				}else{
			 					$.messager.alert('友情提示','<br/>请选择一条记录！','warning');
			 				}
					   }
				   }
				],
				onAfterEdit:function(rowData,changes){
					  var node = $('#test').treegrid('getSelected');
					   if(node){
						   $.ajax({
							   url:'/itmanager/yh/qxwhcz?tag='+tag,
							   type:'post',
							   data:node,
							   success:function(data){
								   var parentNode = $('#test').treegrid('getParent',node.id);
								   if(parentNode){
								  		$('#test').treegrid('reload',parentNode.id);
								   }else{
									   $('#test').treegrid('options').queryParams = null;
									   $('#test').treegrid('reload');
								   } 
								   $.messager.alert('友情提示','<br/>保存成功！','right');
							   },
							   error:function(data){
									 $.messager.alert('友情提示','<br/>保存失败！','error');
							   }
						   });
					   }
					   tag = null;
					   editId = null;
				},
				onClickRow: function(rowData){
					if(editId){
						 $("#test").treegrid('unselectAll');
						 $('#test').treegrid('select',editId);
					}
				}
			});
			
			$('#gnsz').dialog({
				title:'菜单功能设置',
				iconCls:'icon-edit',
				buttons:'#dlg-buttons',
				width:400,
				height:400,
				closed:true
			});
			
			var ens='${param.en}';
			 var en=ens.split(":");
			 for(var i=0;i<en.length;i++){
				 if(en[i] != "" && en[i] != null){
				 	$("#"+en[i]).linkbutton('enable');
				 }
			 }
			
		});
		
		clsDlg = function(id){
			$('#'+id).dialog('close');
		}
		
		submitForm = function(iframeid,formid){
		    var jifm = $(window.frames[iframeid].document);
			jifm.find("#"+formid).form("submit",{ 
				url:'/itmanager/yh/qxgnwhbc',
				success:function(data){
					$.messager.alert('友情提示','<br/>保存成功！','ok');
					$('#gnsz').dialog('close');
					var v = $('#test').treegrid('getSelected');
					var vv = $('#test').treegrid('getParent',v.id);
					$("#test").treegrid('reload',vv.id);
				},
				onLoadError:function(){
					$.messager.alert('友情提示','<br/>保存失败！','error');
				},
				onSubmit:function(){
					var v = jifm.find("input:checked");
					if(!v){
						$.messager.alert('友情提示','<br/>请先选择功能！','warning');
						return false;
					}
				}
			});
		}
	</script>
</head>
<body class="easyui-layout">
	<div id="center" region="center" border="false">
		<table id="test"></table>
	</div>
	<div id="gnsz">
		<iframe id="jtcz" frameborder="0" scrolling="auto" style="width:100%;height:100%;"></iframe>
		<div id="dlg-buttons">
			<a class="easyui-linkbutton" href="javascript:void(0)" onclick="submitForm('jtcz','czForm')" id="btn-save"
				icon="icon-ok">保存</a> 
			<a class="easyui-linkbutton"  href="javascript:void(0)"
				onclick="clsDlg('gnsz')" id="btn-cancel" icon="icon-undo">取消</a>
		</div> 
	</div>
</body>
</html>